package com.ipod.ipod2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;


public class NowPlaying extends Activity {// implements Runnable {

	private boolean currentTrackHasLyrics = false;
	private boolean currentTrackHasAlbumCover = true;
	
	private boolean isShuffled = false;
	
	//Child Ids for the view flipper
	private final int FLIP_PROGRESS_ID = 0;
	private final int FLIP_SEEK_ID = 1;
	private final int FLIP_RATING_ID = 2;
	private final int FLIP_LYRICS_ID = 3;
	private final int FLIP_ART_ID = 4;	
	
	private ProgressBar pgBar;
	private SeekBar sgBar;
	private TextView timeInProgress;
	private TextView timeLeftProgress;
	private TextView timeInSeek;
	private TextView timeLeftSeek;
	
	private int songLength;
	
	private Handler mViewHandler = new Handler();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		//Set the view to now playing layout
        setContentView(R.layout.now_playing);
        
        //Set up listeners
        Button flipButton = (Button) findViewById(R.id.flipButton);
        flipButton.setOnClickListener(button_listener);
        
        //TODO Extract the data from the MP3
        //TODO populate the title, artist, album name, rating, lyrics, art etc in all the views
        //so when the view changes the data will be correct
        songLength = 100;
        
        //Set up seek bar move listener
        sgBar = (SeekBar) findViewById (R.id.trackSeekBar);
        sgBar.setProgress(0);
        sgBar.setMax(songLength); //mp.getDuration();
        sgBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){   
        	@Override   
        	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {    
        		// TODO Auto-generated method stub    
        		if(fromUser) {
        			//mp.seekTo(progress);
        			pgBar.setProgress(progress);
        		}
        	}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
        	
        });
        
        
        //TODO previous, next, play/pause, menu buttons
        
        if(!isShuffled) {
        	ImageView shuffleIcon = (ImageView) findViewById(R.id.shuffleImage);
        	shuffleIcon.setVisibility(View.INVISIBLE);
        }
        

        
        //Set up the timers
        //TODO format the string with seconds timer eg 3:45 etc
        timeInProgress = (TextView) findViewById(R.id.currentTimeInProgress);
        timeLeftProgress = (TextView) findViewById(R.id.timeLeftProgress);
        timeInSeek = (TextView) findViewById(R.id.currentTimeInSeek);
        timeLeftSeek = (TextView) findViewById(R.id.timeLeftSeek);
        timeInProgress.setText("0");
        timeInSeek.setText("0");
        timeLeftProgress.setText(String.valueOf(songLength));
        timeLeftSeek.setText(String.valueOf(songLength));
        
        //Set up thread to handle updates to the progress and seek bars 
        //and time elapsed and remaining counters
        pgBar = (ProgressBar) findViewById(R.id.trackProgressBar);
        pgBar.setProgress(0);
        pgBar.setMax(songLength); //mp.getDuration();
        
        Thread background = new Thread(new Runnable() {
        	public void run() {
        		try {
        			while(pgBar.getProgress() < pgBar.getMax()) {
        				Thread.sleep(1000);
        				mProgressHandler.sendMessage(mProgressHandler.obtainMessage());
        			}        			
        		} catch (InterruptedException e) {
        			return;
        		}
        	}
        });
        background.start();
        
        //TODO finally start playing the media
        
        
	}
	
	Handler mProgressHandler = new Handler() {
		public void handleMessage(Message msg) {
			pgBar.incrementProgressBy(1);
			sgBar.incrementProgressBy(1);
	        timeInProgress.setText(String.valueOf(pgBar.getProgress()));
	        timeInSeek.setText(String.valueOf(sgBar.getProgress()));
	        timeLeftProgress.setText('-' + String.valueOf(songLength - pgBar.getProgress()));
	        timeLeftSeek.setText('-' + String.valueOf(songLength - sgBar.getProgress()));
		}
	};
	
	
	
    private OnClickListener button_listener = new OnClickListener() {
    	public void onClick(View v) {
    		ViewFlipper vf = (ViewFlipper) findViewById(R.id.nowPlayingFlipper);
    		   		   		
    		switch(vf.getCurrentView().getId()) {
    		case R.id.now_playing_flip_track_progress:    			
    			mViewHandler.removeCallbacks(mUpdateTimeTask);
    			vf.setDisplayedChild(FLIP_SEEK_ID);    			
    			mViewHandler.postDelayed(mUpdateTimeTask, 5000);
    			break;
    		case R.id.now_playing_flip_track_seek:
    			mViewHandler.removeCallbacks(mUpdateTimeTask);
    			if(currentTrackHasLyrics) {
    				vf.setDisplayedChild(FLIP_LYRICS_ID);    				
    			} else if (currentTrackHasAlbumCover){
    				vf.setDisplayedChild(FLIP_ART_ID);
    			} else {
    				vf.setDisplayedChild(FLIP_RATING_ID);
    				mViewHandler.postDelayed(mUpdateTimeTask, 5000);
    			}
    			break;
    		case R.id.now_playing_flip_track_lyrics:
    			mViewHandler.removeCallbacks(mUpdateTimeTask);
    			if(currentTrackHasAlbumCover) {
    				vf.setDisplayedChild(FLIP_ART_ID);
    			} else {
    				vf.setDisplayedChild(FLIP_RATING_ID);
    				mViewHandler.postDelayed(mUpdateTimeTask, 5000);
    			}
    			break;
    		case R.id.now_playing_flip_track_art:
    			mViewHandler.removeCallbacks(mUpdateTimeTask);
    			vf.setDisplayedChild(FLIP_RATING_ID);
    			mViewHandler.postDelayed(mUpdateTimeTask, 5000);
    			break;
    		case R.id.now_playing_flip_track_rating:
    			mViewHandler.removeCallbacks(mUpdateTimeTask);
    			vf.setDisplayedChild(FLIP_PROGRESS_ID);
    			break;
    		}
    		
    		
    		
    	}
    };

    private Runnable mUpdateTimeTask = new Runnable () {
    	public void run() {
    		ViewFlipper vf = (ViewFlipper) findViewById(R.id.nowPlayingFlipper);
    		
    		if(vf.getCurrentView().getId() == R.id.now_playing_flip_track_seek ||
    		vf.getCurrentView().getId() == R.id.now_playing_flip_track_rating) {
    			vf.setDisplayedChild(FLIP_PROGRESS_ID);
    		}
    		mViewHandler.removeCallbacks(mUpdateTimeTask);
    	}
    };
    
}
